package gestionCandidats;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.SortedSet;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

import modele.Bd;
import modele.Parcours;
import modele.UniteEnseignement;

public class UniteEnseignementMajAction extends AbstractAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GestionCandidats appli;
	private UniteEnseignementAfficheAction parent;

	private JPanel vue, pied, panel;
	private JScrollPane scrollpane;
	private JCheckBox[][] jcb;
	private SortedSet<Parcours> lParcours;

	protected UniteEnseignementMajAction(GestionCandidats appli, UniteEnseignementAfficheAction ueaa) {
		super("UniteEnseignementMajAction");
		this.appli = appli;
		parent = ueaa;
		vue = appli.getUniteEnseignementMaj();
	}

	private String[] getEntete() throws Exception {
		String[] entete = new String[3];
		entete[0] = "Obligatoire";
		entete[1] = "Choix 1";
		entete[2] = "Choix 2";
		return entete;
	}

	public void actionPerformed(ActionEvent e) {
		try {
			UniteEnseignement ue =parent.getSelectedUniteEnseignement();
			if (ue != null) {
				parent.getVue().setVisible(false);
				if(vue == null) {
					// l'info
					vue = new JPanel();
					vue.setLayout(new BorderLayout());
					appli.add(vue,BorderLayout.CENTER);

					pied = new JPanel();
					JButton cancel = new JButton("Fermer");
					cancel.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent e) {
							vue.setVisible(false);
							parent.getVue().setVisible(true);
						}
					});
					pied.add(cancel);
					vue.add(pied, BorderLayout.SOUTH);

					panel = new JPanel();
					panel.setLayout(new GridLayout(0,3));
					scrollpane = new JScrollPane();
					scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
					panel.add(scrollpane,BorderLayout.CENTER);
					vue.add(panel, BorderLayout.CENTER);
				}
				panel.removeAll();
				panel.add(new JLabel(ue.getIntitule()));
				panel.add(new JLabel(ue.getSigle()));
				panel.add(new JLabel(ue.getQuota().toString()));
				Bd bd = appli.getBd();
				lParcours = bd.getParcours();
				String[] names = getEntete();
				for (int i=0; i<names.length; i++)
					panel.add(new JLabel(names[i]));
				jcb = new JCheckBox[lParcours.size()][names.length];
				int i = 0;
				for (Parcours par : lParcours) {
					jcb[i][0] = new JCheckBox(par.getSigle(),ue.getParcoursObligatoire().contains(par));
					jcb[i][0].setEnabled(false);
					panel.add(jcb[i][0]);
					jcb[i][1] = new JCheckBox(par.getSigle(),ue.getParcoursChoix1().contains(par));
					jcb[i][1].setEnabled(false);
					panel.add(jcb[i][1]);
					jcb[i][2] = new JCheckBox(par.getSigle(),ue.getParcoursChoix2().contains(par));
					jcb[i][2].setEnabled(false);
					panel.add(jcb[i][2]);
					i++;
				}
				vue.setVisible(true);
			}
		}
		catch(Exception ex){
			JOptionPane.showMessageDialog(appli, ex.getMessage(), "Mise a jour des UE : Attention",JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}
	}
}
